<?php
# Sample script for RAWS tutorial 10: see http://rambla.eu/ for more info.
#
# Copyright (C) 2010 rambla.be

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require_once '../RawsClient/Raws/Rass.php';

# Provide the path to a local testfile, that can be uploaded via RASS
define('LOCAL_FILE', '/my/path/to/a/file.mp4');

# Provide your own RASS credentials here
define('USER', 'xxx'); # your user account name
define('PWD', 'xxx'); # your user account pwd
define('CDN', "cdn0x"); # your sub-cdn (e.g. "cdn01")

define('BASE', 'http://rass.' . CDN . '.rambla.be/');

# all RASS API methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
try {
  # Instantiate an object that manages the RASS connection, passing it your login credentials and the base service uri
  $rass = new Rass(USER, PWD, BASE);

  # Create a "tutorial10" directory under the root-directory
  $dir_entry = $rass->createDir("tutorial10", True);
  echo "Created directory with path: " . $dir_entry->path . "\n";
  
  # Upload a file 'test.mp4' to your dir
  $item_entry = $rass->postItem($dir_entry->path, "test.mp4", LOCAL_FILE);
  echo "Created file with path: " . $item_entry->path . "\n";

  # Set the following element: <meta vocab="dc" meta_name="title">The lion sleeps tonight</title>
  echo "\nSetting metadata for " . $item_entry->kind . " entry with path = " . $item_entry->path . " (<meta vocab='dc' meta_name='title'>The lion sleeps tonight</title>)\n";
  $meta_elems = array(new Rass_Extension_Meta("dc", "title", "The lion sleeps tonight"));
  $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(), $meta_elems);
  foreach ($meta_entry->content->params->meta as $t) {
    echo "vocab: " . $t->vocab . "\n";
    echo "meta_name: " . $t->meta_name . "\n";
    echo "value: " . $t->text . "\n";
  }

  # Add the following element: <meta vocab="media" meta_name="price" type="rent" price="19.99" currency="EUR" />
  echo "\nAdding meta element to " . $meta_entry->kind . " entry with path = " . $meta_entry->path . " (<meta vocab='media' meta_name='price' type='rent' price='19.99' currency='EUR' />)\n";
  $meta_elems = array(new Rass_Extension_Meta("media", "price", null, array("type" => "rent", "price" => "19.99", "currency" => "EUR")));
  $meta_entry = $rass->addMetadata($meta_entry, array(), $meta_elems);
  foreach ($meta_entry->content->params->meta as $t) {
    echo "\nvocab: " . $t->vocab . "\n";
    echo "meta_name: " . $t->meta_name . "\n";
    if ($t->text) {
      echo "value: " . $t->text . "\n";
    }
    foreach ($t->attrs as $key => $value) {
      echo "Attr: " . $key . "=" . $value . "\n";
    }
  }
  
  # To replace a data inside an existing meta element, you can modify the array of Rass_Extension_Meta objects...
  echo "\nChanging price and adding subscription info for " . $meta_entry->kind . " entry with path = " . $meta_entry->path . "\n";
  $meta_entry = $rass->getMetaEntry($item_entry->path);
  foreach ($meta_entry->content->params->meta as $t) {
    if (($t->meta_name == "price") && ($t->vocab == "media")) {
      $a = $t->attrs;
      $a["price"] = "20.00";
      $a["info"] = "http://www.dummy.jp/subscription_info";
      $t->attrs = $a;
    }
  }
  # ... followed by a call to addMetadata() to POST our meta_entry (you can also add new elements at the same time) 
  $meta_entry = $rass->addMetadata($meta_entry, array(), array());
  foreach ($meta_entry->content->params->meta as $t) {
    echo "\nvocab: " . $t->vocab . "\n";
    echo "meta_name: " . $t->meta_name . "\n";
    if ($t->text) {
      echo "value: " . $t->text . "\n";
    }
    foreach ($t->attrs as $key => $value) {
      echo "Attr: " . $key . "=" . $value . "\n";
    }
  }
      
  # Note: If you use setMetadata() or setMetadataFromPath(), existing meta elements will be removed (only the ones passed in the argument are being set)
  echo "\nReplacing metadata for " . $meta_entry->kind . " entry with path = " . $meta_entry->path . ". Adding custom 'myvocab' elements.\n";
  $meta_elems = array();
  $meta_elems[] = new Rass_Extension_Meta("myvocab", "animal", "lion", array("description" => "sleeping at night"));
  $meta_elems[] = new Rass_Extension_Meta("myvocab", "like", null, array("likes" => "1520", "dislikes" => "2"));
  $meta_entry = $rass->setMetadata($meta_entry, array(), $meta_elems);
  foreach ($meta_entry->content->params->meta as $t) {
    echo "\nvocab: " . $t->vocab . "\n";
    echo "meta_name: " . $t->meta_name . "\n";
    if ($t->text) {
      echo "value: " . $t->text . "\n";
    }
    foreach ($t->attrs as $key => $value) {
      echo "Attr: " . $key . "=" . $value . "\n";
    }
  }
  
    // # Delete all metadata for this entry (if you only have the relative path, use deleteMetaEntry_from_path() instead)
  // echo "\nDeleting manual metadata for " . $meta_entry->kind . " entry with path = " . $meta_entry->path . "\n";
  // $rass->deleteMetaEntry($meta_entry);
  // # We can still retrieve the meta entry, but it will not contain any tags
  // $meta_entry = $rass->getMetaEntry($item_entry->path);
  // echo "\nAll metadata for " . $meta_entry->kind . " entry with path = " . $meta_entry->path . " has been removed !\n";
  // foreach ($meta_entry->content->params->meta as $t) {
  //   echo "\nvocab: " . $t->vocab . "\n";
  //   echo "meta_name: " . $t->meta_name . "\n";
  //   if ($t->text) {
  //     echo "value: " . $t->text . "\n";
  //   }
  //   foreach ($t->attrs as $key => $value) {
  //     echo "Attr: " . $key . "=" . $value . "\n";
  //   }
  // }
  // 
  // 
  // # DELETE directory on the CDN. Set second argument to True to delete recursively.
  // $rass->deleteDir($dir_entry->path, True);
  // echo "Deleted dir: " . $dir_entry->path . "\n";
  

}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Raws::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
